package com.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.task.bean.Student;
import com.task.bean.Task;
import com.task.bean.TaskExec;
import com.task.mapper.StudentMapper;
import com.task.mapper.TaskExecMapper;
import com.task.service.ITaskExecService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.task.vo.TaskAndExecVo;
import com.task.vo.TaskExecQuery;
import com.task.vo.TitleAndTaskExecVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzhe
 * @since 2021-04-10
 */
@Service
public class TaskExecServiceImpl extends ServiceImpl<TaskExecMapper, TaskExec> implements ITaskExecService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TaskExecMapper taskExecMapper;

    @Override
    public List<TaskExec> queryTaskExecAndStu(int teaId,TaskExec taskExec) {
        List<Student> studentList = studentMapper.queryStuByTeaId(teaId);
        List<Integer> collect = studentList.stream().map(stu -> stu.getId()).collect(Collectors.toList());
        return taskExecMapper.queryTaskExecByStuIds(collect);
    }

    @Override
    public void queryTaskExecAndStuIds(List<Integer> collect) {

    }

    @Override
    public List<TitleAndTaskExecVo> queryTaskExecListByStuId(TaskExecQuery execQuery,int teaId) throws ParseException {
        List<TitleAndTaskExecVo> execVoList = taskExecMapper.queryTaskExecListByStuId(teaId);
        if (StringUtils.isNotEmpty(execQuery.getName())) {
            execVoList = execVoList.stream().filter(item -> execQuery.getName().equals(item.getStuName())).collect(Collectors.toList());
        }
        if (StringUtils.isNotEmpty(execQuery.getStartDate())&&StringUtils.isNotEmpty(execQuery.getEndDate())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = sdf.parse(execQuery.getStartDate().replaceAll("/","-"));
            Date endDate = sdf.parse(execQuery.getEndDate().replaceAll("/","-"));
            execVoList = execVoList.stream().filter(item -> dateFilter(startDate,endDate, item)).distinct().collect(Collectors.toList());
        }
        if (ObjectUtils.isNotEmpty(execQuery.getMinNum())&&ObjectUtils.isNotEmpty(execQuery.getMaxNum())) {
            execVoList = execVoList.stream().filter(item -> (execQuery.getMinNum() < item.getScore() && item.getScore() < execQuery.getMaxNum())).distinct().collect(Collectors.toList());
        }
        return execVoList;
    }

    private boolean dateFilter(Date startDate, Date endDate,TitleAndTaskExecVo item) {
        if (startDate.before(item.getCreateTime())&& endDate.after(item.getCreateTime())) {
            return true;
        }
        return false;
    }

    @Override
    public List<TaskAndExecVo> queryTaskExecAndTaskByStuId(int stuId) {
        return taskExecMapper.queryTaskExecAndTaskByStuId(stuId);
    }

}
